﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvModel;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ApplicantDB
    {
        private static CvModelContainer _context = new CvModelContainer();

        //CREATE
        public static int CreateApplicant(Applicant applicant)
        {
            try
            {
                var query = _context.Applicants.Add(applicant);
                return _context.SaveChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        //READ
        public static Applicant GetApplicant(string personNumber, string password)
        {
            try
            {
                var query =
                    (from a in _context.Applicants
                     where a.PersonNumber == personNumber && a.Password == password
                     select new Applicant { FirstName = a.FirstName, LastName = a.LastName, PersonNumber = a.PersonNumber, Email = a.Email, Phone = a.Phone, Address = a.Address, Zip = a.Zip, City = a.City, Cv_CvId = a.Cv_CvId, ApplicantId = a.ApplicantId, Cv = a.Cv, IsDeleted = a.IsDeleted, RegistredCv = a.RegistredCv}).ToList().First();

                return query;

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public static bool ApplicantIsLoggedIn(string personNumber, string password)
        {
            try
            {
                var query = _context.Applicants.Where(a => a.PersonNumber == personNumber && a.Password == password).ToList().First();

                return query != null;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //UPDATE
        public static int UpdateApplicant(Applicant applicant)
        {
            try
            {
                _context.Applicants.AddOrUpdate(applicant);
                return _context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //DELETE
        public static int DeleteApplicant(int applicantId)
        {
            try
            {
                var query =
                    (from a in _context.Applicants
                     where a.ApplicantId == applicantId
                     select a).Single();

                _context.Applicants.Remove(query);

                return _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
